#include <memory>
#include <iostream>
#include <fstream>

int main() {
    // 使用Lambda表达式作为自定义删除器
    std::unique_ptr<std::ofstream, void(*)(std::ofstream*)> logFile(
        new std::ofstream("log.txt"),
        [](std::ofstream* p) {
            p->close(); // 正确关闭文件
            delete p;   // 释放内存
            std::cout << "File closed and memory freed.\n";
        }
    );

    *logFile << "Log message" << std::endl;
}